package com.android.kotlin.repository

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MediatorLiveData
import com.android.kotlin.bean.GankBean
import com.android.kotlin.base.BaseRepository
import com.android.kotlin.util.Constant
import com.android.kotlin.util.Resource
import com.android.kotlin.util.addCaches
import com.android.kotlin.util.cache.CacheUtils
import com.android.kotlin.util.http.ApiService
import com.android.kotlin.util.http.RetrofitFactory
import com.android.kotlin.util.rxMain
import com.android.kotlin.util.taskscheduler.TaskScheduler
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * @Description:
 * @Author: xuwenlong
 * @Email: xuwenlong @ebrun.com
 * @Version:
 * @Create: 2019/2/26 0026
 * @Modify:
 */

class MainRepository : BaseRepository(){

    private val pageSize = 20
    val apiService by lazy { RetrofitFactory.instance.getService(
        ApiService::class.java,
        Constant.API_URL) }


    fun getGril():LiveData<Resource<GankBean>>{
        return object:NetworkRepository<GankBean>(){
            override fun shouldCache(): Boolean {
                return true
            }
            override fun loadFromDb(): LiveData<GankBean> {
                var liveData=MediatorLiveData<GankBean>()
                TaskScheduler.execute {
                    var bean = CacheUtils.getInstance().getSerializable(cacheId())
                    try {
                        liveData.postValue(bean as GankBean?)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                return liveData
            }

            override fun createCall(): LiveData<Resource<GankBean>> {
                return getGirlList(0,true)
            }
        }.asLiveData
    }


    fun getGirlList(num:Int,addCache:Boolean):LiveData<Resource<GankBean>>{
        val liveData=MediatorLiveData<Resource<GankBean>>()
       apiService.getGirlList(pageSize,num)
           .addCaches{
               if(addCache){
                   Schedulers.io().createWorker().schedule {
                       if (it != null || it?.results!!.isEmpty()) {
                           CacheUtils.getInstance().put(cacheId(), it)
                       }
                   }
               }
           }
           .rxMain()
            .subscribe(object:io.reactivex.Observer<GankBean>{
                override fun onComplete() {
                }
                override fun onSubscribe(d: Disposable) {
                    addSubscribe(d)
                }

                override fun onNext(t: GankBean) {
                    if (t == null || t.results!!.isEmpty()) {
                        liveData.value= Resource.empty()
                    } else {
                        liveData.value= Resource.success(t)
                    }
                }
                override fun onError(e: Throwable) {
                    liveData.setValue(Resource.error("啊",null))
                }
            })
        return liveData
    }

     fun cacheId():String {
        return "dalongCache"
    }
}